package com.harzone.commons.search.domain;

import java.util.Arrays;
import java.util.Collection;

import com.harzone.commons.search.exception.SearchException;

/**
 * 过滤条件
 * @author ls
 * 2014年11月21日下午2:11:35
 */
public class Condition extends Where implements Cloneable{

	/**
	 * 操作符
	 * @author ls
	 * 2014年11月21日下午2:24:06
	 */
	public static enum OPEAR {
		EQ("=","等于")
		,EQ1("==","等于")
		, GT(">","大于")
		, LT("<","小于")
		, GTE(">=","大于等于")
		, LTE("<=","小于等于")
		, NEQ("!=","不等于")
		, N("<>","不等于")
		, LIKE("like","等于")
		, NLIKE("not like","NOT LIKE")
		, IS("is","等于")
		, ISN("is not","IS NOT")
		, IN("in")
		, NIN("not in","NOT IN")
		, BETWEEN("between")
		, NBETWEEN("not between","NOT BETWEEN")
		, UNKOWN("","未知");
		
		private String name = "";
		private String desc = "";
		
		private OPEAR(){
			
		}
		private OPEAR(String name){
			this.name = name;
		}
		private OPEAR(String name,String desc){
			this.name = name;
			this.desc = desc;
		}
		public String getName() {
			return name;
		}
		public String getDesc() {
			return desc;
		}
		public static OPEAR get(String name){
			OPEAR[] opears = values();
			for (OPEAR opear : opears) {
				if(opear.getName().equalsIgnoreCase(name)){
					if(opear==EQ1){
						return OPEAR.EQ;
					}
					return opear;
				}
			}
			return UNKOWN;
		}
	};

	protected String name = "";

	protected Object value = null;
	
	protected Object value1 = null;

	protected OPEAR opear = OPEAR.EQ;
	
	protected Condition(CONN conn, String name, OPEAR oper, Object value) {
		this(conn, name, oper, value, null);
	}

	protected Condition(CONN conn, String name, OPEAR opear, Object value, Object value1) {
		super(conn);
		this.name = name;
		this.value = value;
		this.value1 = value1;
		this.opear = opear;
	}

	protected Condition(CONN conn, String name, String oper, Object value) throws SearchException {
		this(conn, name, OPEAR.get(oper), value);
	}

	public String getName() {
		return name;
	}

	public Condition setName(String name) {
		this.name = name;
		return this;
	}

	public Object getValue() {
		return value;
	}

	public Condition setValue(Object value) {
		this.value = value;
		return this;
	}

	public OPEAR getOpear() {
		return opear;
	}

	/**
	 * 操作符
	 * @author ls
	 * 2014年11月22日下午4:06:58 
	 * @param opear
	 * @return
	 */
	public Condition setOpear(OPEAR opear) {
		this.opear = opear;
		return this;
	}


	/**
	 * 操作符
	 * @author ls
	 * 2014年11月22日下午4:06:48 
	 * @param opear
	 * @return
	 */
	public Condition setOpear(String opear) {
		this.opear = OPEAR.get(opear);
		return this;
	}
	
	
	/**
	 * in条件
	 * @author ls
	 * 2014年11月24日下午2:49:02 
	 * @param name
	 * @param values
	 * @return
	 */
	public static Condition newInInstance(String name,String... values){
		return new Condition(CONN.AND, name, OPEAR.IN, values);
	}


	/**
	 * in条件
	 * @author ls
	 * 2014年11月24日下午2:48:53 
	 * @param name
	 * @param values
	 * @return
	 */
	public static Condition newInInstance(String name,Collection values){
		return new Condition(CONN.AND, name, OPEAR.IN, values);
	}
	
	
	/**
	 * 新条件（and）
	 * @author ls
	 * 2014年11月22日下午4:07:14 
	 * @param name
	 * @param oper
	 * @param value
	 * @return
	 */
	public static Condition newInstance(String name, String oper, Object value){
		return new Condition(CONN.AND, name, oper, value);
	}
	
	
	/**
	 * 新条件
	 * @author ls
	 * 2014年11月22日下午4:07:29 
	 * @param conn
	 * @param name
	 * @param oper
	 * @param value
	 * @return
	 */
	public static Condition newInstance(CONN conn, String name, String oper, Object value){
		return new Condition(conn, name, oper, value);
	}
	
	
	/**
	 * 新条件,默认为等于运算符
	 * @author ls
	 * 2014年11月22日下午4:07:32 
	 * @param conn
	 * @param name
	 * @param value
	 * @return
	 */
	public static Condition newInstance(CONN conn, String name, Object value){
		return new Condition(conn, name,  OPEAR.EQ, value);
	}

	/**
	 * 新条件（and）,默认为等于运算符
	 * @author ls
	 * 2014年11月22日下午4:07:36 
	 * @param name
	 * @param value
	 * @return
	 */
	public static Condition newInstance(String name, Object value){
		return new Condition(CONN.AND, name,  OPEAR.EQ, value);
	}
	
	
	/**
	 * 新条件（and）Between ... and ...
	 * @author ls
	 * 2014年11月22日下午4:07:41 
	 * @param name
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static Condition newBetween(String name, Object value1, Object value2){
		return new Condition(CONN.AND, name, OPEAR.BETWEEN, value1, value2);
	}
	

	/**
	 * 新条件Between ... and ...
	 * @author ls
	 * 2014年11月22日下午4:07:45 
	 * @param conn
	 * @param name
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static Condition newBetween(CONN conn, String name, Object value1, Object value2){
		return new Condition(conn, name, OPEAR.BETWEEN, value1, value2);
	}
	

	/**
	 * （and）not Between ... and ...
	 * @author ls
	 * 2014年11月22日下午4:07:48 
	 * @param name
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static Condition newNotBetween(String name, Object value1, Object value2){
		return new Condition(CONN.AND, name, OPEAR.NBETWEEN, value1, value2);
	}
	

	/**
	 * not Between ... and ...条件
	 * @author ls
	 * 2014年11月22日下午4:07:56 
	 * @param conn
	 * @param name
	 * @param value1
	 * @param value2
	 * @return
	 */
	public static Condition newNotBetween(CONN conn, String name, Object value1, Object value2){
		return new Condition(conn, name, OPEAR.NBETWEEN, value1, value2);
	}
	
	@Override
	public String toString() {

		if (value instanceof Object[]) {
			return this.conn + " " + this.name + " " + this.opear + " " + Arrays.toString((Object[]) value);
		} else if (this.opear == OPEAR.BETWEEN || this.opear == OPEAR.NBETWEEN) {
			return this.conn + " " + this.name + " " + this.opear + " " + this.value+" "+this.value1;
		}else {
			return this.conn + " " + this.name + " " + this.opear + " " + this.value;
		}
	}

	public Object getValue1() {
		return value1;
	}

	public Condition setValue1(Object value1) {
		this.value1 = value1;
		return this;
	}

}
